package com.tg.app.helper;

import android.text.TextUtils;

import com.tange.base.toolkit.PreferenceUtil;
import com.tg.appcommon.android.TGApplicationBase;
import com.tg.appcommon.android.TGLog;
import com.tange.base.toolkit.TGThreadPool;
import com.tg.data.bean.DeviceItem;
import com.tg.data.bean.TGGlobalConfig;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class TGGlobalConfigHelper {

    private static final String TAG = "TGGlobalConfigHelper";

    public static TGGlobalConfigHelper getInstance() {
        return SingletonHolder.sInstance;
    }

    private TGGlobalConfig mGlobalConfig = new TGGlobalConfig();

    public boolean isShowLockBellDialog() {
        return showLockBellDialog;
    }

    public void setShowLockBellDialog(boolean showLockBellDialog) {
        this.showLockBellDialog = showLockBellDialog;
    }

    public String getLockbellServiceDetail(){
        return mGlobalConfig.getLockbellServiceDetail();
    }

    public String getBirdfeederServiceDetail(){
        return mGlobalConfig.getBirdfeederServiceDetail();
    }


    private boolean showLockBellDialog = false;

    private TGGlobalConfigHelper() {

    }

    public String getLearning_assistance(){
        return mGlobalConfig.getLearning_assistance();
    }

    public String getDevice_battery_usage_stats(){
        return mGlobalConfig.getDevice_battery_usage_stats();
    }

    public String getLearning_assistance_service(){
        return mGlobalConfig.getLearning_assistance_service();
    }

    public String getGuidelineWhileAddingDevice(){
        return mGlobalConfig.getGuidelineWhileAddingDevice();
    }

    public String getHowToResetDevice(){
        return mGlobalConfig.getHowToResetDevice();
    }

    public String getSettingsAccount(){
        return mGlobalConfig.getSettingsAccount();
    }

    public String getStorageServiceDetailDoorbell(){
        return mGlobalConfig.getStorageServiceDetailDoorbell();
    }

    public String getThaiLandServicePriceList(){
        return mGlobalConfig.getThaiLandServicePriceList();
    }
    public String getSim_check() {
        return mGlobalConfig.getSim_check();
    }

    public String getLockBellOperationLog(){
        return mGlobalConfig.getLockBellOperationLog();
    }

   public String getHelpUrl(DeviceItem deviceItem,boolean isBad){
        if (isBad){
            return mGlobalConfig.getBad_network();
        }
        if (DeviceHelper.is4GDevice(deviceItem)){
            return mGlobalConfig.getSim_disconnected();
        }
       return mGlobalConfig.getWifi_disconnected();
    }

    public TGGlobalConfig config() {
        return mGlobalConfig;
    }

    public String getSimServiceDetail(){
        return mGlobalConfig.getSimServiceDetail();
    }

    public String getSuperAiService(){
        return mGlobalConfig.getSuperAiService();
    }

    public String getAiServiceDetail(){
        return mGlobalConfig.getAiServiceDetail();
    }

    public String getTimeZoneUrl(){
        return mGlobalConfig.getTimezone();
    }


    public String getContact_us_link(){
        return mGlobalConfig.getContact_us_link();
    }

    public String getAbout_us_link(){
        return mGlobalConfig.getAbout_us_link();
    }

    public String getWork_order_link(){
        return mGlobalConfig.getWork_order_link();
    }
    public boolean isShow_contact_us(){
        return mGlobalConfig.isShow_contact_us();
    }

    public boolean isShow_about_us(){
        return mGlobalConfig.isShow_about_us();
    }


    public String getPermissionSettingGuideUrl() {
        if (mGlobalConfig != null && !TextUtils.isEmpty(mGlobalConfig.getDeveloperServiceUrl())) {
            final String url = mGlobalConfig.getDeveloperServiceUrl();
            TGThreadPool.execute(() -> PreferenceUtil.setString(TGApplicationBase.getApplicationContext(), TGGlobalConfig.KEY_DEVELOPER_SERVICE_URL, url));
            return url;
        } else {
            String url = PreferenceUtil.getString(TGApplicationBase.getApplicationContext(), TGGlobalConfig.KEY_DEVELOPER_SERVICE_URL);
            return url;
        }
    }

    public String getDeveloperServiceUrl() {
        if (mGlobalConfig != null && !TextUtils.isEmpty(mGlobalConfig.getDeveloperServiceUrl())) {
            final String url = mGlobalConfig.getDeveloperServiceUrl();
            TGThreadPool.execute(() -> PreferenceUtil.setString(TGApplicationBase.getApplicationContext(), TGGlobalConfig.KEY_DEVELOPER_SERVICE_URL, url));
            return url;
        } else {
            String url = PreferenceUtil.getString(TGApplicationBase.getApplicationContext(), TGGlobalConfig.KEY_DEVELOPER_SERVICE_URL);
            return url;
        }
    }


    private static class SingletonHolder {
        private static TGGlobalConfigHelper sInstance = new TGGlobalConfigHelper();
    }

    public String getShareUrl(){
        return mGlobalConfig.getShare();
    }
    public boolean isAddDeviceByScanAp(){
        return mGlobalConfig.isAddDeviceByScanAp();
    }

    public boolean isUseWifiDefaultQuality() {
        int q = PreferenceUtil.getInt(TGApplicationBase.getApplicationContext(), TGGlobalConfig.KEY_WIFI_DEFAULT_QUALITY, 0);
        return q == 1;
    }

    public String getStorageServiceDetail(){
        return mGlobalConfig.getStorageServiceDetail();
    }


    private void setWifiDefaultQuality(int q) {
        PreferenceUtil.setInt(TGApplicationBase.getApplicationContext(), TGGlobalConfig.KEY_WIFI_DEFAULT_QUALITY, q);
    }

    public void getAddDeviceVersionFromServer(final AppConfigListener listener) {
        HashMap<String, String> map = new HashMap<>();
        TGHttp.getInstance().getGloalConfig(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<TGGlobalConfig>() {
                    @Override
                    protected void onSuccess(TGGlobalConfig content) {

                        TGLog.i(TAG, "[onSuccess] " + content);
                        mGlobalConfig = content;

                        setWifiDefaultQuality(content.getWifiQuality());
                        if (listener != null){
                            listener.onSuccess();
                        }
                    }
                });
    }

    public interface AppConfigListener {
        void onSuccess();
    }

}
